clear all
set maxvar 12000
global toppath "C:\Users\micha\Dropbox\ShadowPills\Replication Package JFQA\"
global path "$toppath\Created Datasets"
cd "$path"
//Written by Lubo Litov 
//Use Stacked Regression DiD estimator as in Matsa and Gormley (2016, JFE)

//##############################
// Cohorts (t-3, t+3)
//##############################

clear all
eststo clear

//(t-3, t+3) Cohorts
//#1: "MN"
use "$path/PPL_toanalyze", clear
keep if year!=.&year>=1992&year<=1998 //event year is 1995
g cohortid="MN"
save cohort_MN.dta, replace
//#2: "WA"
use "$path/PPL_toanalyze", clear
keep if year!=.&year>=1995&year<=2001 //event year is 1998
g cohortid="WA"
save cohort_WA.dta, replace
//#3: "SC"
use "$path/PPL_toanalyze", clear
keep if year!=.&year>=1995&year<=2001 //event year is 1998
g cohortid="SC"
save cohort_SC.dta, replace
//#4: "MD"
use "$path/PPL_toanalyze", clear
keep if year!=.&year>=1996&year<=2002 //event year is 1999
g cohortid="MD"
save cohort_MD.dta, replace
//#5: "MO"
use "$path/PPL_toanalyze", clear
keep if year!=.&year>=1996&year<=2002 //event year is 1999
g cohortid="MO"
save cohort_MO.dta, replace
//#6: "MI"
use "$path/PPL_toanalyze", clear
keep if year!=.&year>=1998&year<=2004 //event year is 2001
g cohortid="MI"
save cohort_MI.dta, replace
//#7: "ME"
use "$path/PPL_toanalyze", clear
keep if year!=.&year>=1999&year<=2005 //event year is 2002
g cohortid="ME"
save cohort_ME.dta, replace
//#8: "TX"
use "$path/PPL_toanalyze", clear
keep if year!=.&year>=1999&year<=2005 //event year is 2002
g cohortid="TX"
save cohort_TX.dta, replace
//#9: "CT"
use "$path/PPL_toanalyze", clear
keep if year!=.&year>=2000&year<=2006 //event year is 2003
g cohortid="CT"
save cohort_CT.dta, replace
//#10: "MS"
use "$path/PPL_toanalyze", clear
keep if year!=.&year>=2002&year<=2008 //event year is 2005
g cohortid="MS"
save cohort_MS.dta, replace
//#11: "VT"
use "$path/PPL_toanalyze", clear
keep if year!=.&year>=2005&year<=2011 //event year is 2008
g cohortid="VT"
save cohort_VT.dta, replace
//#11: "WY"
use "$path/PPL_toanalyze", clear
keep if year!=.&year>=2006&year<=2012 //event year is 2009
g cohortid="WY"
save cohort_WY.dta, replace

//Append the cohorts for second-wave PP statutes
use cohort_MN.dta, clear
append using cohort_WA.dta
append using cohort_SC.dta
append using cohort_MD.dta
append using cohort_MO.dta
append using cohort_MI.dta
append using cohort_ME.dta
append using cohort_TX.dta
append using cohort_CT.dta
append using cohort_MS.dta
append using cohort_VT.dta
append using cohort_WY.dta
save cohort_data3.dta, replace

use cohort_data3.dta, clear
egen cohort_gvkey=group(cohortid gvkey)
egen cohort_sic3h_fyear=group(cohortid sich3 fyear)
egen cohort_div_fyear=group(cohortid division fyear)
egen cohort_region_fyear=group(cohortid region fyear)
egen cohort_stathq_fyear=group(cohortid state fyear)

eststo clear
eststo: reghdfe lq_w5 pplh1 pplh_ppillatadopt bclh1 cslh1 ddlh1 fplh1 if otc==0 & (backfill==0 | inrange(flag,1,4))  &  inrange(year,1992,2012) & Lp_pill!=., absorb(cohort_gvkey cohort_sic3h_fyear cohort_div_fyear) cluster(cohortid incs fyear)

//##############################
*Generate cohorts (t-5, t+5)
//##############################

clear all
eststo clear

//(t-3, t+3) Cohorts
//#1: "MN"
use "$path/PPL_toanalyze", clear
keep if year!=.&year>=1990&year<=2000 //event year is 1995
g cohortid="MN"
save cohort_MN5.dta, replace
//#2: "WA"
use "$path/PPL_toanalyze", clear
keep if year!=.&year>=1993&year<=2003 //event year is 1998
g cohortid="WA"
save cohort_WA5.dta, replace
//#3: "SC"
use "$path/PPL_toanalyze", clear
keep if year!=.&year>=1993&year<=2003 //event year is 1998
g cohortid="SC"
save cohort_SC5.dta, replace
//#4: "MD"
use "$path/PPL_toanalyze", clear
keep if year!=.&year>=1994&year<=2004 //event year is 1999
g cohortid="MD"
save cohort_MD5.dta, replace
//#5: "MO"
use "$path/PPL_toanalyze", clear
keep if year!=.&year>=1994&year<=2004 //event year is 1999
g cohortid="MO"
save cohort_MO5.dta, replace
//#6: "MI"
use "$path/PPL_toanalyze", clear
keep if year!=.&year>=1996&year<=2006 //event year is 2001
g cohortid="MI"
save cohort_MI5.dta, replace
//#7: "ME"
use "$path/PPL_toanalyze", clear
keep if year!=.&year>=1997&year<=2007 //event year is 2002
g cohortid="ME"
save cohort_ME5.dta, replace
//#8: "TX"
use "$path/PPL_toanalyze", clear
keep if year!=.&year>=1997&year<=2007 //event year is 2002
g cohortid="TX"
save cohort_TX5.dta, replace
//#9: "CT"
use "$path/PPL_toanalyze", clear
keep if year!=.&year>=1998&year<=2008 //event year is 2003
g cohortid="CT"
save cohort_CT5.dta, replace
//#10: "MS"
use "$path/PPL_toanalyze", clear
keep if year!=.&year>=2000&year<=2010 //event year is 2005
g cohortid="MS"
save cohort_MS5.dta, replace
//#11: "VT"
use "$path/PPL_toanalyze", clear
keep if year!=.&year>=2003&year<=2013 //event year is 2008
g cohortid="VT"
save cohort_VT5.dta, replace
//#11: "WY"
use "$path/PPL_toanalyze", clear
keep if year!=.&year>=2004&year<=2014 //event year is 2009
g cohortid="WY"
save cohort_WY5.dta, replace

//Append the cohorts for second-wave PP statutes
use cohort_MN5.dta, clear
append using cohort_WA5.dta
append using cohort_SC5.dta
append using cohort_MD5.dta
append using cohort_MO5.dta
append using cohort_MI5.dta
append using cohort_ME5.dta
append using cohort_TX5.dta
append using cohort_CT5.dta
append using cohort_MS5.dta
append using cohort_VT5.dta
append using cohort_WY5.dta
save cohort_data5.dta, replace

use cohort_data5.dta, clear
egen cohort_gvkey=group(cohortid gvkey)
egen cohort_sic3h_fyear=group(cohortid sich3 fyear)
egen cohort_div_fyear=group(cohortid division fyear)
egen cohort_region_fyear=group(cohortid region fyear)
egen cohort_stathq_fyear=group(cohortid state fyear)

eststo: reghdfe lq_w5 pplh1 pplh_ppillatadopt bclh1 cslh1 ddlh1 fplh1 if otc==0 & (backfill==0 | inrange(flag,1,4))  &  inrange(year,1992,2012) & Lp_pill!=., absorb(cohort_gvkey cohort_sic3h_fyear cohort_div_fyear) cluster(cohortid incs fyear)


//##############################
*Generate cohorts (t-10, t+10)
//##############################

clear all
eststo clear

//(t-10, t+10) Cohorts
//#1: "MN"
use "$path/PPL_toanalyze", clear
keep if year!=.&year>=1985&year<=2005 //event year is 1995
g cohortid="MN"
save cohort_MN10.dta, replace
//#2: "WA"
use "$path/PPL_toanalyze", clear
keep if year!=.&year>=1988&year<=2008 //event year is 1998
g cohortid="WA"
save cohort_WA10.dta, replace
//#3: "SC"
use "$path/PPL_toanalyze", clear
keep if year!=.&year>=1988&year<=2008 //event year is 1998
g cohortid="SC"
save cohort_SC10.dta, replace
//#4: "MD"
use "$path/PPL_toanalyze", clear
keep if year!=.&year>=1989&year<=2009 //event year is 1999
g cohortid="MD"
save cohort_MD10.dta, replace
//#5: "MO"
use "$path/PPL_toanalyze", clear
keep if year!=.&year>=1989&year<=2009 //event year is 1999
g cohortid="MO"
save cohort_MO10.dta, replace
//#6: "MI"
use "$path/PPL_toanalyze", clear
keep if year!=.&year>=1991&year<=2011 //event year is 2001
g cohortid="MI"
save cohort_MI10.dta, replace
//#7: "ME"
use "$path/PPL_toanalyze", clear
keep if year!=.&year>=1992&year<=2012 //event year is 2002
g cohortid="ME"
save cohort_ME10.dta, replace
//#8: "TX"
use "$path/PPL_toanalyze", clear
keep if year!=.&year>=1992&year<=2012 //event year is 2002
g cohortid="TX"
save cohort_TX10.dta, replace
//#9: "CT"
use "$path/PPL_toanalyze", clear
keep if year!=.&year>=1993&year<=2013 //event year is 2003
g cohortid="CT"
save cohort_CT10.dta, replace
//#10: "MS"
use "$path/PPL_toanalyze", clear
keep if year!=.&year>=1995&year<=2015 //event year is 2005
g cohortid="MS"
save cohort_MS10.dta, replace
//#11: "VT"
use "$path/PPL_toanalyze", clear
keep if year!=.&year>=1998&year<=2018 //event year is 2008
g cohortid="VT"
save cohort_VT10.dta, replace
//#11: "WY"
use "$path/PPL_toanalyze", clear
keep if year!=.&year>=1999&year<=2019 //event year is 2009
g cohortid="WY"
save cohort_WY10.dta, replace

//Append the cohorts for second-wave PP statutes
use cohort_MN10.dta, clear
append using cohort_WA10.dta
append using cohort_SC10.dta
append using cohort_MD10.dta
append using cohort_MO10.dta
append using cohort_MI10.dta
append using cohort_ME10.dta
append using cohort_TX10.dta
append using cohort_CT10.dta
append using cohort_MS10.dta
append using cohort_VT10.dta
append using cohort_WY10.dta
save cohort_data10.dta, replace

use cohort_data10.dta, clear
egen cohort_gvkey=group(cohortid gvkey)
egen cohort_sic3h_fyear=group(cohortid sich3 fyear)
egen cohort_div_fyear=group(cohortid division fyear)
egen cohort_region_fyear=group(cohortid region fyear)
egen cohort_stathq_fyear=group(cohortid state fyear)

eststo: reghdfe lq_w5 pplh1 pplh_ppillatadopt bclh1 cslh1 ddlh1 fplh1 if otc==0 & (backfill==0 | inrange(flag,1,4))  &  inrange(year,1992,2012) & Lp_pill!=., absorb(cohort_gvkey cohort_sic3h_fyear cohort_div_fyear) cluster(cohortid incs fyear)

esttab using "$toppath/Results/Tab6_Stacked.csv", keep(pplh* ) replace stats(N r2_a) star(+ 0.15 * 0.1 ** 0.05 *** 0.01)
